package com.cheyunkeji.er.fragment.auction;

import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyun.netsalev3.MyApplication;
import com.cheyunkeji.er.MyFragmentManager;
import com.cheyunkeji.er.adapter.MyFragmentPagerAdapter;
import com.cheyunkeji.er.base.BaseFragment;
import com.cheyunkeji.er.bean.MsgCountWrapper;
import com.cheyunkeji.er.utils.TabLayoutAdjustBoundUtils;
import com.cheyunkeji.er.view.TopBar;
import com.google.android.material.tabs.TabLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;


public class GivePriceFragment extends BaseFragment {

    @BindView(R.id.v_topbar)
    TopBar vTopbar;
    @BindView(R.id.tl_title)
    TabLayout tlTitle;
    @BindView(R.id.vp_frag_content)
    ViewPager vpFragContent;
    @BindView(R.id.tv_item_count)
    TextView tvItemCount;

    private ArrayList<String> titleList;
    private ArrayList<Fragment> fragList;
    private MyFragmentPagerAdapter adapter;

    @Override
    protected void init() {
        EventBus.getDefault().register(this);
        fragList = new ArrayList<>();
        titleList = new ArrayList<>();
        titleList.add(MyApplication.getInstance().getString(R.string.wait_for_give));
        titleList.add(MyApplication.getInstance().getString(R.string.gave_price_alerady));
        titleList.add(MyApplication.getInstance().getString(R.string.not_give_price));
    }

    @Override
    protected void initView() {
        vTopbar.setTitle("报价");
        fragList.add(MyFragmentManager.getInstance().getFrag(GivePriceWaitFragment.class));
        fragList.add(MyFragmentManager.getInstance().getFrag(GivePriceGaveFragment.class));
        fragList.add(MyFragmentManager.getInstance().getFrag(GivePriceNotFragment.class));
        adapter = new MyFragmentPagerAdapter(getActivity().getSupportFragmentManager(), titleList, fragList);
        vpFragContent.setAdapter(adapter);
        tlTitle.setupWithViewPager(vpFragContent);
        tlTitle.setTabsFromPagerAdapter(adapter);
//       调整TabLayou中tab之间间距
        TabLayoutAdjustBoundUtils.setIndicator(getActivity(), tlTitle, 30, 30);
//        设置TabLayout中tab之间分隔线
        TabLayoutAdjustBoundUtils.setTabDivider(tlTitle);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void doInflaterView(LayoutInflater inflater, ViewGroup container) {
        contentView = inflater.inflate(R.layout.er_frag_giveprice_layout, container, false);
        ButterKnife.bind(this, contentView);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMsgCountChanged(MsgCountWrapper wrapper) {

        if (vpFragContent.getCurrentItem() == wrapper.pageIndex) {
            setItemCount(wrapper.msgCount);
        }

    }

    public void setItemCount(int itemCount) {
        tvItemCount.setText(String.format("共%1$s条数据", String.valueOf(itemCount)));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

}
